/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity;

import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.chaoticengine.cgll.serialization.CircleConverter;
import org.chaoticengine.cgll.serialization.ColorConverter;
import org.chaoticengine.cgll.serialization.RectangleConverter;
import org.chaoticengine.cgll.serialization.Vector2fConverter;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Vector2f;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.convert.Registry;
import org.simpleframework.xml.convert.RegistryStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.Strategy;

/**
 * The EntityFactory is a singleton class through which all Entity objects are
 * created, loaded, saved and destroyed.
 *
 * @author Matt v.d. Westhuizen
 */
public class EntityFactory {
    private static EntityFactory instance = null;

    protected List<Entity> entities = null;
    protected Registry registry = null;
    protected Strategy strategy = null;
    protected Serializer serializer = null;
    private int nextId = 0;

    private EntityFactory() {
        entities = new ArrayList<Entity>();

        registry = new Registry();
        strategy = new RegistryStrategy(registry);
        serializer = new Persister(strategy);

        try {
            registry.bind(Vector2f.class, Vector2fConverter.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error: Failed to register SimpleXML converter for Vector2f.");
        }

        try {
            registry.bind(Rectangle.class, RectangleConverter.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error: Failed to register SimpleXML converter for Rectangle.");
        }

        try {
            registry.bind(Circle.class, CircleConverter.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error: Failed to register SimpleXML converter for Circle.");
        }

        try {
            registry.bind(Color.class, ColorConverter.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error: Failed to register SimpleXML converter for Color.");
        }
    }

    public static EntityFactory getInstance() {
        if (instance == null) {
            instance = new EntityFactory();
        }

        return(instance);
    }

    public Entity createEntity() {
        Entity result = new Entity();
        result.setId(nextId++);
        entities.add(result);
        return(result);
    }

    public Entity loadEntity(String filename) throws Exception {
        File fil = new File(filename);
        Entity result = serializer.read(Entity.class, fil);
        result.setId(nextId++);
        entities.add(result);
        return(result);
    }

    public void saveEntity(Entity e, String filename) throws Exception {
        File fil = new File(filename);
        serializer.write(e, fil);
    }

    public void destroyEntity(Entity e) {
        entities.remove(e);
        e.removeAllListeners();
        //e.destroy();
        //e.getComponentManager().destroy();
    }
}
